// <copyright file="YUIAspNet.Master.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Xml;

namespace YUIAspNetDemo
{
    public partial class YUIAspNetMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Url.IsLoopback) //.Host.ToLower().IndexOf("localhost") > -1)
            {
                this.plAnalytics.Visible = 
                    this.plAdsense.Visible = 
                    this.lnkDonate.Visible = false;

                this.plDownloadMenu.Visible = true;
            }
            else
            {
                this.plAnalytics.Visible =
                                   this.plAdsense.Visible =
                                   this.lnkDonate.Visible = true;

                this.plDownloadMenu.Visible = false;
            }
            BindTree();
        }

        string pagename = "";

        private void BindTree()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/Web.sitemap"));

            XmlElement root = doc.DocumentElement;

            foreach (XmlNode xmlNode in root.ChildNodes[0].ChildNodes)
            {
                XmlElement elem = xmlNode as XmlElement;
                if (elem != null)
                {
                    YUIAspNet.TreeNode node = new YUIAspNet.TreeNode();
                    node.Expanded = GetExpandState(elem.Attributes["title"].Value);
                    node.Text = elem.Attributes["title"].Value;
                    
                    foreach (XmlNode xmlLeafNode in elem.ChildNodes)
                    {
                        XmlElement leafElem = xmlLeafNode as XmlElement;
                        if (leafElem != null)
                        {
                            YUIAspNet.TreeNode leafNode = new YUIAspNet.TreeNode();
                            
                            leafNode.IsLeaf = true;
                            leafNode.Text = leafElem.Attributes["title"].Value;
                            leafNode.Tooltip = leafElem.Attributes["description"].Value;
                            leafNode.NavigateUrl = new Uri(VirtualPathUtility.ToAbsolute(leafElem.Attributes["url"].Value), UriKind.Relative);
                            if (Request.Url == leafNode.NavigateUrl)
                                pagename = leafNode.Text;
                            node.Nodes.Add(leafNode);
                        }
                    }
                    treeControls.Nodes.Add(node);
                }
            }
        }

        private bool GetExpandState(string nodeName)
        {
            foreach (string urlPart in Request.RawUrl.Split('/'))
            {
                if (string.Compare(nodeName, urlPart, true) == 0)
                    return true;
            }
            return false;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            // demo
            bread.AddTrail("YUIAsp.Net Controls Demo", "/livedemo");

            //find out control's name
            string[] splitUrl = Request.RawUrl.Split('/');
            string controlName = splitUrl[splitUrl.Length - 2];

            if (Request.RawUrl.ToLower().IndexOf("/default.aspx") > -1) // home of control..
            {
                bread.AddTrail(controlName, "");
            }
            else
            {
                string controlPage = splitUrl[splitUrl.Length - 1].Replace(".aspx","");
                splitUrl[splitUrl.Length - 1] = "default.aspx";
                string controlUrl = string.Join("/", splitUrl);

                bread.AddTrail(controlName, controlUrl);

                bread.AddTrail(pagename, "");
            }
            bread.RenderTrail();

    
            base.Render(writer);
        }
    }
}
